<?php

class user {

    protected $_userid;   // KEY ATTR. WITH AUTOINCREMENT
    protected $_username;
    protected $_password;
    protected $_firstname;
    protected $_lastname;
	protected $_dealergroup;
    protected $_email;
	protected $_phone;
	protected $_mobile;
	protected $_shippingaddress;
	protected $_billingaddress;
	protected $_createddate;
	protected $_isadmin;
    protected $_status;

    /**
     *   @desc   CONSTRUCTOR METHOD
     */
    function __construct() {
        global $DBOBJ;
        $this->_obj = $DBOBJ;
        $this->_userid = null;
        $this->_username = null;
        $this->_password = null;
        $this->_firstname = null;
        $this->_lastname = null;
		$this->_dealergroup = null;
        $this->_email = null;
		$this->_phone= null;
		$this->_mobile = null;
		$this->_shippingaddress = null;
		$this->_billingaddress = null;
		$this->_createddate = null;
		$this->_isadmin = 0;
        $this->_status = null;
    }

    /**
     *   @desc   DECONSTRUCTOR METHOD
     */
    function __destruct() {
        unset($this->_obj);
    }

    /**
     *   @desc   GETTER METHODS
     */
    public function getuserid() {
        return $this->_userid;
    }

    public function getusername() {
        return $this->_username;
    }

    public function getpassword() {
        return $this->_password;
    }

    public function getfirstname() {
        return $this->_firstname;
    }

    public function getlastname() {
        return $this->_lastname;
    }
	
	public function getdealergroup() {
        return $this->_dealergroup;
    }

    public function getemail() {
        return $this->_email;
    }
	
	public function getphone() {
        return $this->_phone;
    }
		
	public function getmobile() {
        return $this->_mobile;
    }
	
	public function getshippingaddress() {
        return $this->_shippingaddress;
    }
	
	public function getbillingaddress() {
        return $this->_billingaddress;
    }
	
	public function getcreateddate() {
        return $this->_createddate;
    }

	public function getisadmin() {
        return $this->_isadmin;
    }
	
    public function getstatus() {
        return $this->_status;
    }

    /**
     *   @desc   SETTER METHODS
     */
    public function setuserid($userid) {
        $this->_userid = $userid;
    }

    public function setusername($username) {
        $this->_username = $username;
    }

    public function setpassword($password) {
        $this->_password = $password;
    }

    public function setfirstname($firstname) {
        $this->_firstname = $firstname;
    }

    public function setlastname($lastname) {
        $this->_lastname = $lastname;
    }
	
	public function setdealergroup($dealergroup) {
        $this->_dealergroup = $dealergroup;
    }

    public function setemail($email) {
        $this->_email = $email;
    }
	
	public function setphone($phone) {
        $this->_phone = $phone;
    }

	public function setmobile($mobile) {
        $this->_mobile = $mobile;
    }

	public function setshippingaddress($shippingaddress) {
        $this->_shippingaddress = $shippingaddress;
    }

	public function setbillingaddress($billingaddress) {
        $this->_billingaddress = $billingaddress;
    }

	public function setcreateddate($createddate) {
        $this->_createddate = $createddate;
    }
	
	
	public function setisadmin($isadmin) {
        $this->_isadmin = $isadmin;
    }
	
    public function setstatus($status) {
        $this->_status = $status;
    }

    function select($id) {
        $sql = "SELECT * FROM user WHERE userid = '" . $id . "'";
        $row = $this->_obj->select($sql);

        $this->_userid = $row[0]['userid'];
        $this->_username = $row[0]['username'];
        $this->_password = $row[0]['password'];
        $this->_firstname = $row[0]['firstname'];
        $this->_lastname = $row[0]['lastname'];
		$this->_lastname = $row[0]['dealergroup'];
        $this->_email = $row[0]['email'];
		$this->_phone = $row[0]['phone'];
		$this->_mobile = $row[0]['mobile'];
		$this->_shippingaddress = $row[0]['shippingaddress'];
		$this->_billingaddress = $row[0]['billingaddress'];
		$this->_createddate = $row[0]['createddate'];
		$this->_isadmin = $row[0]['isadmin'];
        $this->_status = $row[0]['status'];
        return $row['0'];
    }


    /**
     *   @desc   set All
     */
    public function setAll($dataArray = array()) {
	if (count($dataArray) <= 0) {
	    return;
	}

	foreach ($dataArray as $key => $value) {
	    $classKey = '_' . $key;
	    $this->$classKey = $value;
	}
    }

    public function getAll($FORMAT = 'ARRAY') {
	$dataArray = new stdClass();
	$dataArray->userid = $this->getuserid();
	$dataArray->username = $this->getusername();
	$dataArray->password = $this->getpassword();
	$dataArray->firstname = $this->getfirstname();
	$dataArray->lastname = $this->getlastname();
	$dataArray->dealergroup = $this->getdealergroup();
	$dataArray->email = $this->getemail();
	$dataArray->phone = $this->getphone();
	$dataArray->mobile = $this->getmobile();
	$dataArray->shippingaddress = $this->getshippingaddress();
	$dataArray->billingaddress = $this->getbillingaddress();
	$dataArray->createddate = $this->getcreateddate();
	$dataArray->isadmin = $this->getisadmin();
	$dataArray->status = $this->getstatus();
	if ($FORMAT == 'ARRAY') {
	    return (array) $dataArray;
	}
	return $dataArray;
    }


    /**
     *   @desc   DELETE
     */
    function delete($id) {
        $sql = "DELETE FROM user WHERE userid = '" . $id . "'";
        $result = $this->_obj->sql_query($sql);
    }

    /**
     *   @desc   INSERT
     */
    function insert() {
        $this->_userid = ""; // clear key for autoincrement
        $sql = "INSERT INTO  `user`
		(`userid`,`username`,`password`,`firstname`,`lastname`,`dealergroup`,`email`,`phone`,`mobile`,`shippingaddress`,`billingaddress`,`createddate`,`isadmin`,`status`)
		VALUES (NULL,
		'" . $this->_username . "',
		'" . $this->_password . "',
		'" . $this->_firstname . "',
		'" . $this->_lastname . "',
		'" . $this->_dealergroup . "',
		'" . $this->_email . "',
		'" . $this->_phone . "',
		'" . $this->_mobile . "',
		'" . $this->_shippingaddress . "',
		'" . $this->_billingaddress . "',
		'" . $this->_createddate . "',
		'" . $this->_isadmin . "',
		'" . $this->_status . "')";
        $result = $this->_obj->insert($sql);
        return $result;
    }

    /**
     *   @desc   UPDATE
     */
    function update($id) {

        $sql = "UPDATE `user` SET
			`userid` = '" . $this->_userid . "',
			`username` = '" . $this->_username . "',
			`password` = '" . $this->_password . "',
			`firstname` = '" . $this->_firstname . "',
			`lastname` = '" . $this->_lastname . "',
			`dealergroup` = '" . $this->_dealergroup . "',
			`email` = '" . $this->_email . "',
			`phone` = '" . $this->_phone . "',
			`mobile` = '" . $this->_mobile . "',
			`shippingaddress` = '" . $this->_shippingaddress . "',
			`billingaddress` = '" . $this->_billingaddress . "',
			`createddate` = '" . $this->_createddate . "',
			`isadmin` = '" . $this->_isadmin . "',
			`status` = '" . $this->_status . "'
		WHERE `userid` = '" . $this->_userid . "'";

        $result = $this->_obj->sql_query($sql);
    }

    public function getAllUserList($params = '*', $where = '', $orderby = "id", $limit = '') {
	$orderby = 'ORDER BY ' . $orderby;
	$sql = "SELECT " . $params . " FROM `user` a  " . $where . " " . $orderby . " " . $limit;
	$row = $this->_obj->select($sql);
	if ($row) {
	    return $row;
	}
	return array();
    }

    function checkUserCredentials($username = '', $password = '') {
        $password = base64_encode($password);
        $sql = "select userid,username,firstname,lastname,email from user where `email` = '" . $username . "' and `password` = '" . $password . "' and `isadmin` = '1' and `status` = 'Active' limit 1";
        $row = $this->_obj->select($sql);
        if (count($row) > 0) {
            return $row['0'];
        }
        return false;
    }

    function checkOldPassword($userId, $oldpassword) {
        $userArray = $this->select($userId);
        $userPasword = $userArray['password'];
        $userPasword = base64_decode($userPasword);
        $oldpassword;
        if ($userPasword == $oldpassword) {
            return true;
        }
        return false;
    }

    function changePassword($password = '') {
        $password = base64_encode($password);
        $sql = "UPDATE `user` SET
			`password` = '" . $password . "'
		WHERE `userid` = '" . $this->_userid . "'";
        $result = $this->_obj->sql_query($sql);
    }

    function changeEmail($email = '') {
        $sql = "UPDATE `user` SET
			`email` = '" . $email . "'
		WHERE `userid` = '" . $this->_userid . "'";
        $result = $this->_obj->sql_query($sql);
    }

    function checkEmailExists($email = '') {
        $sql = "select * from user where email = '" . $email . "' limit 1";
        $row = $this->_obj->select($sql);
        if (count($row) > 0) {
            return $row['0'];
        }
        return false;
    }

    function checkEmail($currentEmail = '' ,$email ) {
        $sql = "select * from user where email = '" . $currentEmail . "' or email = '".$email."' ";
        $row = $this->_obj->select($sql);
        if (count($row) > 1) {
            return false;
        }
        return true;
    }

    public function performListAction($action = '', $ids = "''") {
	switch ($action) {
	    case 'delete':
		$sql = "DELETE FROM `user` WHERE `userid` IN (" . $ids . ")";
		break;
	    default:
		$sql = '';
	}
	if ($sql == '') {
	    return false;
	}
	$result = $this->_obj->sql_query($sql);
	return true;
    }			public function checkDealerGroupinUser($dealerId = "") {		if ($dealerId == '') {			return;		}		$sql = "SELECT count(*) as 'cnt' FROM user WHERE dealergroup REGEXP(".$dealerId.")"; 		$result = $this->_obj->select($sql);		return $result['0']['cnt'];	}

    public function getCountUser( $where = "") {
	$sql = "SELECT count(*) as cnt  FROM `user`" . $where;
	$result = $this->_obj->select($sql);
	return $result['0']['cnt'];
    }

}

?>